﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using UniCT.LinkShareWpf.Interface;
using System.Windows.Data;
using UniCT.LinkShareWpf.Runtime;

namespace LinkShareWpf.Model
{
    public class ResourceViewModel : INotifyPropertyChanged
    {
        private ICollectionView view;
      
        public ResourceViewModel(MyResources resources)
        {
            this.view = CollectionViewSource.GetDefaultView(resources.Resources);

        }


        #region Public implementation

        public String Director
        {
            get { return this.CurrentResource.CInfo.Director; }
            set
            {
                if (value != this.CurrentResource.CInfo.Director)
                {
                    this.CurrentResource.CInfo.Director = value;
                    this.OnPropertyChanged("Director");
                }
            }
        }

        public String Title
        {
            get { return this.CurrentResource.CInfo.Title; }
            set
            {
                if (value != this.CurrentResource.CInfo.Title)
                {
                    this.CurrentResource.CInfo.Title = value;
                    this.OnPropertyChanged("Title");
                }
            }
        }

        public String Storyline
        {
            get { return this.CurrentResource.CInfo.Storyline; }
            set
            {
                if (value != this.CurrentResource.CInfo.Storyline)
                {
                    this.CurrentResource.CInfo.Storyline = value;
                    this.OnPropertyChanged("Storyline");
                }
            }
        }

        public EnumGenre Genre
        {
            get { return this.CurrentResource.CInfo.Genre; }
            set
            {
                if (value != this.CurrentResource.CInfo.Genre)
                {
                    this.CurrentResource.CInfo.Genre = value;
                    this.OnPropertyChanged("Genre");
                }
            }
        }

        public EnumLanguage Language
        {
            get { return this.CurrentResource.CInfo.Language; }
            set
            {
                if (value != this.CurrentResource.CInfo.Language)
                {
                    this.CurrentResource.CInfo.Language = value;
                    this.OnPropertyChanged("Language");
                }
            }
        }

        public EnumLanguage ? SubTitleLanguage
        {
            get { return this.CurrentResource.CInfo.SubTitleLanguage; }
            set
            {
                if (value != this.CurrentResource.CInfo.SubTitleLanguage)
                {
                    this.CurrentResource.CInfo.SubTitleLanguage = value;
                    this.OnPropertyChanged("SubTitleLanguage");
                }
            }
        }

        public UInt32 Year
        {
            get { return this.CurrentResource.CInfo.Year; }
            set
            {
                if (value > 0 && value != this.CurrentResource.CInfo.Year)
                {
                    this.CurrentResource.CInfo.Year = value;
                    this.OnPropertyChanged("Year");
                }
            }
        }

        public UInt32 Duration
        {
            get { return this.CurrentResource.CInfo.Duration; }
            set
            {
                if (value > 0 && value != this.CurrentResource.CInfo.Duration)
                {
                    this.CurrentResource.CInfo.Duration = value;
                    this.OnPropertyChanged("Duration");
                }
            }
        }

        public String CoverName
        {
            get;
            set;
        }

        public CastCollection Cast
        {
            get { return this.CurrentResource.CInfo.Cast; }
            set
            {
                this.CurrentResource.CInfo.Cast = value;
                this.OnPropertyChanged("Cast");
            }
        }

        public String EpisodTitle
        {
            get { return (this.CurrentResource.CInfo as ContentInfoTVS).EpisodTitle; }
            set
            {
                if (value != (this.CurrentResource.CInfo as ContentInfoTVS).EpisodTitle)
                {
                    (this.CurrentResource.CInfo as ContentInfoTVS).EpisodTitle = value;
                    this.OnPropertyChanged("EpisodTitle");
                }
            }
        }

        public UInt32 EpisodNumber
        {
            get { return (this.CurrentResource.CInfo as ContentInfoTVS).EpisodNumber; }
            set
            {
                if (value > 0 && value != (this.CurrentResource.CInfo as ContentInfoTVS).EpisodNumber)
                {
                    (this.CurrentResource.CInfo as ContentInfoTVS).EpisodNumber = value;
                    this.OnPropertyChanged("EpisodNumber");
                }
            }
        }

        public UInt32 SeasonNumber
        {
            get { return (this.CurrentResource.CInfo as ContentInfoTVS).SeasonNumber; }
            set
            {
                if (value > 0 && value != (this.CurrentResource.CInfo as ContentInfoTVS).SeasonNumber)
                {
                    (this.CurrentResource.CInfo as ContentInfoTVS).SeasonNumber = value;
                    this.OnPropertyChanged("SeasonNumber");
                }
            }
        }

        public UInt32 MegaByte
        {
            get { return this.CurrentResource.TInfo.MegaByte; }
            set
            {
                if (value > 0 && value != this.CurrentResource.TInfo.MegaByte)
                {
                    this.CurrentResource.TInfo.MegaByte = value;
                    this.OnPropertyChanged("MegaByte");
                }
            }
        }

        public EnumVideoType VideoType
        {
            get { return this.CurrentResource.TInfo.VideoType; }
            set
            {
                if (value != this.CurrentResource.TInfo.VideoType)
                {
                    this.CurrentResource.TInfo.VideoType = value;
                    this.OnPropertyChanged("VideoType");
                }
            }
        }

        public EnumVideoSource VideoSource
        {
            get { return this.CurrentResource.TInfo.VideoSource; }
            set
            {
                if (value != this.CurrentResource.TInfo.VideoSource)
                {
                    this.CurrentResource.TInfo.VideoSource = value;
                    this.OnPropertyChanged("VideoSource");
                }
            }
        }

        public EnumAudioSource AudioSource
        {
            get { return this.CurrentResource.TInfo.AudioSource; }
            set
            {
                if (value != this.CurrentResource.TInfo.AudioSource)
                {
                    this.CurrentResource.TInfo.AudioSource = value;
                    this.OnPropertyChanged("AudioSource");
                }
            }
        }

        public double Rating
        {
            get { return this.CurrentResource.Rating; }
            set
            {
                if (value != this.CurrentResource.Rating)
                {
                    this.CurrentResource.Rating = value;
                    this.OnPropertyChanged("Rating");
                }
            }
        }

        public String Url
        {
            get { return this.CurrentResource.Url; }
            set
            {
                if (value != this.CurrentResource.Url)
                {
                    this.CurrentResource.Url = value;
                    this.OnPropertyChanged("Url");
                }
            }
        }

        #endregion

        #region Private implementation


        private Resource CurrentResource
        {
            get { return this.view.CurrentItem as Resource; }
        }

     
        #endregion        

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        } 

        #endregion
    }
}
